<?php
	
	function makeContent()
	{	
		// default, starting variables
		$feed = "public_timeline.xml";			
		$max_results = 5;
		$user = "";
		$pass = "";
		$tweme = "";
		$loggedin = TRUE;

		// Variables, taken from the user form
			
		if($_COOKIE['twitterReadUser'] != NULL)
			$user = $_COOKIE['twitterReadUser'];
		
		if($_COOKIE['twitterReadPass'] != NULL)
			$pass = $_COOKIE['twitterReadPass'];
			
		if($_COOKIE['twitterReadTweme'] != NULL)
			$tweme = $_COOKIE['twitterReadTweme'];
		
		if($_COOKIE['twitterReadFeed'] == "My own")
			$feed = "user_timeline.xml";			
		elseif($_COOKIE['twitterReadFeed'] == "My friends")
			$feed = "friends_timeline.xml";
		else
			$feed = "public_timeline.xml";
		
		if($_COOKIE['twitterReadNumber'] != NULL)
			$max_results = $_COOKIE['twitterReadNumber'];
		
		// Get the XML data from the Twitter Web Service
		$gettwit = getTwitterFeed($user, $pass, $feed);
		
		// Parse the XML data
		$xml = new XMLReader();
		
		if($gettwit != NULL)
			$xml->XML($gettwit);

		$data = array();
		$ctr = -2;
		$thistype = 0;

		while ($xml->read())
		{
			while($xml->depth<=1    && $xml->nodeType==1)
			{
		  		$thistype = 0;
		  		$xml->read();
		  		$ctr++;
			}
			if ($xml->nodeType==1 && $xml->depth==2)
			{
				$data[$ctr]=array();
				while($xml->depth<2)
				    $xml->read();
				while($xml->depth>=2)
				{
					if($xml->nodeType==1)
				    {
				    	// bit of a hack to make sure an array entry is made, even if a node is empty			    
				   		if($thistype==1)
				    		$data[$ctr][] = "####";
				    		
				    	$thistype = 1;
				    }
				    
				    elseif ($xml->nodeType==3)
				    {
				    	$data[$ctr][] = $xml->value;
				    	$thistype = 3;
				    }
				    
				    $xml->read();
				}
			}	    
		}						
		
		$result = "";
		$i = 0;
		
		// build the output correctly, so it renders like Twitter
		
		// add the block inline CSS content
		$result .= "<style type=\"text/css\">
					.twemepost
					{
						background-color:#FFCC33;
					}
					.userpost
					{
						background-color:#66CCFF;
					}
					</style>";
		
		// add the colour legend
		$result .= "Colour coding:<br />";
		
		$result .= "<div class=\"twemepost\">" . 
					"Posts containing your chosen Tweme have a yellow background" . 
					"</div>";
		
		$result .= "<p />";
		
		$result .= "<div class=\"userpost\">" . 
					"Posts directed specifically at you have a blue background" . 
					"</div>";
		
		$result .= "<p /><hr />";
		
		foreach ($data as $twit)
		{
			if($i < $max_results)
			{
				// check whether the current post contains the chosen tweme or user
				$content = $twit[2];
				
				$thistweme = "#" . $_COOKIE['twitterReadTweme'];
				$twemelength = strlen($thistweme);
								
				$thisuser = "@" . $_COOKIE['twitterReadUser'];
				$userlength = strlen($thisuser);
								
				// add the appropriate div type
				if(strstr($content, $thistweme) AND strlen($thistweme)>1)
				{
					$result .= "<div class=\"twemepost\">";
					$thiscontent = $content;
				}
				elseif(strstr($content, $thisuser) AND strlen($thisuser)>1)
				{
					$result .= "<div class=\"userpost\">";
					$thiscontent = $content;
				}
				else
				{
					$result .= "<div class=\"plainpost\">";
					$thiscontent = $content;
				}
				
				// compile the post content
				
				if($twit[13] != NULL)
					$result .= "<img src=\"" . $twit[15] . "\" alt=\"" . $twit[12] . "\" /><br />"; // avatar
				$result .= "<b>" . $twit[12] . "</b><br />";	// username
				$result .= $thiscontent . "</span><br />";		// post
				$result .= "<font size=\"1\">" . $twit[0] . "</font><br />";	// time and date
				$result .= "<hr />";
				
				$i++;
				
				// close the div tag
				$result .= "</div>";
			}
		}
		
		// return the right stuff
		if($loggedin)
			echo $result;
		else
		{
			$result = "You are not logged in: please login to view your friends' posts.<p /><hr />";
			echo $result;
		}
	}
	
	function getTwitterFeed($username, $password, $xmlfeed)
	{
	   $out  = "GET /statuses/" . $xmlfeed . " HTTP/1.1\r\n";
	   $out .= "Host: twitter.com\r\n";
	   $out .= "Connection: Close\r\n";
	   $out .= "Authorization: Basic ".base64_encode($username . ":" . $password)."\r\n";
	   $out .= "\r\n";
	
	   if (!$conex = fsockopen("twitter.com", 80, $errno, $errstr, 30))
	       return 0;
	   fwrite($conex, $out);
	   $data = '';
	   while (!feof($conex))
	   {
	       $data .= fgets($conex, 512);
	   }
	   fclose($conex);
	   
	   $result_arr = explode("Connection: close\r\n\r\n", $data);
	   $data = $result_arr[1];
	   
	   return $data;
	}
	
	makeContent();
?>